<?php


namespace App\Model;


use Illuminate\Database\Eloquent\Model;

class GoodsSku extends Model
{
    const CREATED_AT = 'createtime';
    const UPDATED_AT = null;

    //表名
    protected $table = 'goods_sku';

    //批量赋值
    protected $fillable = ['goods_id', 'sku_sn', 'bar_code', 'sku_unique', 'value_id', 'spec_value', 'image', 'supply_price', 'line_price', 'weight', 'status'];

    public function getSupplyPriceAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }

    protected function setSupplyPriceAttribute($value)
    {
        return bcmul($value, 100, 0);
    }

    public function getLinePriceAttribute($value)
    {
        return bcdiv($value, 100, 2);
    }

    protected function setLinePriceAttribute($value)
    {
        return bcmul($value, 100, 0);
    }

    /**
     * @param $goods_id
     * @param $spec_list
     * @return array|false
     * @throws \Exception
     */
    public function addSkuList($goods_id, $spec_list)
    {
        $data = [];
        foreach ($spec_list as $key => $item) {
            $data[] = array_merge($item['form'], [
                'goods_id' => $goods_id,
                'value_id' => $item['value_id'],
                'sku_unique' => md5($goods_id . '_' . $this->getValue($item['value_id'])),
                'spec_value' => $this->getValue($item['value_id'])
            ]);
        }
        $goodsSpu = GoodsSpu::find($goods_id);
        $arr = [];
        foreach ($data as $item) {
            array_push($arr, new GoodsSku($item));
        }
        return $goodsSpu->sku()->saveMany($arr);
    }

    /**
     * 添加商品规格关系记录
     * @param $goods_id
     * @param $spec_attr
     * @return array|false
     * @throws \Exception
     */
    public function addGoodsSpecRel($goods_id, $spec_attr)
    {
        $data = [];
        array_map(function ($val) use (&$data, $goods_id) {
            array_map(function ($item) use (&$val, &$data, $goods_id) {
                $data[] = [
                    'goods_id' => $goods_id,
                    'spec_id' => $val['group_id'],
                    'value_id' => $item['item_id'],
                ];
            }, $val['spec_items']);
        }, $spec_attr);
        $goodsSpu = GoodsSpu::find($goods_id);
        $arr = [];
        foreach ($data as $item) {
            array_push($arr, new GoodsSpecRel($item));
        }
        return $goodsSpu->rel()->saveMany($arr);
    }

    /**
     * 移除指定商品的所有sku
     * @param $goods_id
     * @return int
     */
    public function removeAll($goods_id)
    {
        $spec_rel_model = new GoodsSpecRel();
        $spec_rel_model->where('goods_id', $goods_id)->delete();
        return $this->where('goods_id', $goods_id)->delete();
    }

    /**
     * 获取此sku的规格名与规格值
     * @param $value_id
     * @return string
     */
    public function getValue($value_id)
    {
        $valueId = explode(',', $value_id);
        $value = '';
        $model = new SpecValue;
        $arr = [];
        foreach ($valueId as $val) {
            $specValue = $model::find($val)['spec_value'];
            array_push($arr, $specValue);
        }
        $value = implode(',', $arr);
        return $value;
    }
}
